package com.eascs.fp.applyLoan.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.fp.applyLoan.form.ApplyLoanForm;
import com.eascs.fp.applyLoan.server.ApplyLoanService;
import com.eascs.fp.common.entity.ApplyLoanEntity;
import com.eascs.fp.common.entity.ApplyStoreEntity;
import com.eascs.fp.common.entity.MemberEntity;
import com.eascs.fp.common.vo.ModelResult;
import com.eascs.fp.uc.controller.MemberRestController;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;


@RequestMapping("/applyLoan/rest")
@RestController
public class ApplyLoanRestController extends BaseRestController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	
	@Autowired
	private ApplyLoanService applyLoanService;
	
	@Reference
	private SysOrgService sysOrgService;
	
	@RequestMapping(value = "/submit")
	@ResponseBody
	public BaseRestResult<ModelResult<Boolean>> submit(HttpServletRequest request, ApplyLoanForm applyLoanForm) {
		ModelResult<Boolean> modelResult=new ModelResult<>();
		ModelResult<MemberEntity> memberResult=MemberRestController.isLogin(request);
		if (memberResult.getModel() == null) {
			modelResult.withError("applyLoan error", "用户未登录");
			return BaseRestResult.buildRestResult(modelResult);
		}
		MemberEntity memberEntity=memberResult.getModel();
		try {
			ApplyLoanEntity applyLoan=new ApplyLoanEntity();
			ApplyStoreEntity applyStore=new ApplyStoreEntity();
			BeanUtils.copyProperties(applyLoanForm, applyLoan);
			applyLoan.setUserId(memberEntity.getId());
			BeanUtils.copyProperties(applyLoanForm, applyStore);
			modelResult=applyLoanService.applyLoan(applyLoan, applyStore);
			if (modelResult.getModel()) {
				return BaseRestResult.buildRestResult(modelResult);
			}
		} catch (Exception e) {
			logger.info("ApplyLoanRestController.submit error[{}]", e.getMessage());
			modelResult.withError("applyLoan error", "申请贷款错误，请联系管理员");
			return BaseRestResult.buildRestResult(modelResult);
		}
		modelResult.withError("applyLoan error", "申请贷款错误，请联系管理员");
		return BaseRestResult.buildRestResult(modelResult);
	}
	
	
	
	@RequestMapping("/getCooperation")
	@ResponseBody
	public BaseRestResult<String> getCooperation(String province) {
		List<ActOrgEntity> cooperations = sysOrgService.getChildOrgByName(province);
		String json=JSON.toJSONString(cooperations);
		return BaseRestResult.buildRestResult(json);
	}
	

}
